﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;

namespace TireService.Web.Casehandling
{
    public partial class ServiceCarScheduleUsingGoogleMaps : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            if (Page.IsPostBack != true)
            {

            }

            // Bind car data to gridview
            BindCustomerCarsNeedService(db);

            // Dispose connection to database
            db.Dispose();
        }

        /// <summary>
        /// Function that bind customer cars that need service to gridview
        /// </summary>
        /// <param name="db">Connection to database</param>
        private void BindCustomerCarsNeedService(TireServiceDB db)
        {
            //// Initialize google map
            //InitializeGoogleMap();

            // Bind to gridview
            gvCarNeedService.DataSource = db.CustomerCarTireSetDistanceContainerViews.Where(p => p.CarStatusId == (int)Shared.CarStatus.NeedsTireChanged & p.ContainerCargoRoomId != null).OrderBy(p => p.Distance).ToList();
            gvCarNeedService.DataBind();

            gvCarNeedService.GroupBy(gvCarNeedService.Columns["ContainerNumber"]);
            gvCarNeedService.ExpandAll();


        }

        //private void InitializeGoogleMap()
        //{
        //    System.Text.StringBuilder sb = new System.Text.StringBuilder();
        //    sb.Append(@"<script language='javascript'>");
        //    sb.Append(@"google.load('maps', '2.x');");
        //    sb.Append(@"function initialize() {");
        //    sb.Append(@"var map = new google.maps.Map2(document.getElementById('map'));");
        //    sb.Append(@"map.setCenter(new google.maps.LatLng(37.4419, -122.1419), 13);");
        //    sb.Append(@"map.setUIToDefault();");
        //    sb.Append(@"}");
        //    sb.Append(@"google.setOnLoadCallback(initialize);");
        //    sb.Append(@"</script>");

        //    if (!ClientScript.IsStartupScriptRegistered("JSScript"))
        //    {
        //        ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());
        //    }
        //}

        /// <summary>
        /// Gridview function that change field data for unbound fields
        /// </summary>
        protected void gvCarNeedService_CustomUnboundColumnData(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewColumnDataEventArgs e)
        {
            // Format UnBoundColumn LatLong: Show LatLong
            if (e.Column.FieldName == "LatLong")
            {
                // Get latitude and longitude
                string latitude = Convert.ToString(e.GetListSourceFieldValue("LatitudePrimary")).Replace(",", ".");
                string longitude = Convert.ToString(e.GetListSourceFieldValue("LongitudePrimary")).Replace(",", ".");

                // Change field value
                e.Value = latitude + "," + longitude;
            }

            //// Format UnBoundColumn BrandDescription: Show subscription description
            //if (e.Column.FieldName == "BrandDescription")
            //{
            //    // Get brandid
            //    int brandid = Convert.ToInt32(e.GetListSourceFieldValue("CarBrandId"));

            //    TireServiceDB db = new TireServiceDB();

            //    // Change field value
            //    e.Value = db.GetCarBrandDescription(brandid);

            //    db.Dispose();
            //}

            //// Format UnBoundColumn CarStatusDescription: Show car status
            //if (e.Column.FieldName == "CarStatusDescription")
            //{
            //    // Get carstatusid
            //    int carstatusid = Convert.ToInt32(e.GetListSourceFieldValue("CarStatusId"));

            //    TireServiceDB db = new TireServiceDB();

            //    // Change field value
            //    e.Value = db.GetCarStatusDescription(carstatusid);

            //    db.Dispose();
            //}
        }

        /// <summary>
        /// Gridview function that handles button clicks in gridview
        /// </summary>
        protected void gvCarNeedService_CustomButtonCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomButtonCallbackEventArgs e)
        {
            //if (e.ButtonID == "NearbyServiceSchedule")
            //{
            //    try
            //    {
            //        // Get ContainerId
            //        object cellContainerId = gvCarNeedService.GetRowValues(e.VisibleIndex, "ContainerId");

            //        TireServiceDB db = new TireServiceDB();

            //        // Show/Hide needed gridviews
            //        gvPlannedServiceSchedule.Visible = true;
            //        gvNearestCarNeedService.Visible = false;

            //        // Update gridview with planned services
            //        BindPlannedServiceSchedule(db, Convert.ToInt32(cellContainerId));

            //        // Dispose database connection
            //        db.Dispose();
            //    }
            //    catch (Exception ex)
            //    {
            //        if ((ex) is ArgumentException)
            //        {
            //            // User error - arguments not valid - show error message
            //            lMessage.Text = ((ArgumentException)ex).Message;
            //        }
            //        else
            //        {
            //            // System error - show general error message
            //            lMessage.Text = "Fejl opstod - venligst prøv igen";
            //        }
            //    }
            //    finally
            //    {
            //        // Update message panel
            //        upMessage.Update();
            //    }
            //}
        }

        /// <summary>
        /// Gridview function that runs when row has been selectedCarRowIndex
        /// </summary>
        protected void gvCarNeedService_FocusedRowChanged(object sender, EventArgs e)
        {
            //if (Page.IsPostBack != false)
            //{
            //    try
            //    {
            //        // Set longitude/latitude for selected car
            //        this.SelectedCarRowIndex = gvCarNeedService.FocusedRowIndex;

            //        TireServiceDB db = new TireServiceDB();

            //        // Show/Hide needed gridviews
            //        gvPlannedServiceSchedule.Visible = true;
            //        gvNearestCarNeedService.Visible = false;
            //        gvServiceCarScheduleDetail.FocusedRowIndex = -1;
            //        this.SelectedPlannedCarRowIndex = 0;

            //        // Update gridview with planned services and show planned services for selected tireset container location
            //        BindPlannedServiceSchedule(db, Convert.ToInt32(gvCarNeedService.GetRowValues(this.SelectedCarRowIndex, "ContainerId")));

            //        // Dispose database connection
            //        db.Dispose();
            //    }
            //    catch (Exception ex)
            //    {

            //    }
            //}
        }
    }
}